/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.diamondsys.embrapa;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Rafael
 */
@Stateless
public class InteressesFacade extends AbstractFacade<Interesses> {
    @PersistenceContext(unitName = "com.diamondsys_embrapa_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public InteressesFacade() {
        super(Interesses.class);
    }
    
    public List<Interesses> findByClienteId(TbInst clienteId) {
        return em.createQuery(   "  SELECT s FROM Interesses s JOIN s.tbInstList cli WHERE cli.codInstituicao = :tbInstCodInstituicao ", Interesses.class)
                .setParameter("tbInstCodInstituicao", clienteId.getCodInstituicao())
                .getResultList();
    }    
    
    public void removeRelationItem(int interesseId, int clienteId){
        
        em.createNativeQuery("DELETE FROM `nco`.`interesses_inst` WHERE interesses_id = :interesseId AND tb_inst_cod_instituicao = :codCli")
                .setParameter("interesseId", interesseId)
                .setParameter("codCli", clienteId)
                .executeUpdate();
        
    }
    
    public void addRelationItem(int interesseId, int clienteId){
        
        em.createNativeQuery("INSERT INTO `nco`.`interesses_inst` (`interesses_id`, `tb_inst_cod_instituicao`) VALUES (:interesseId,:codCli)")
                .setParameter("interesseId", interesseId)
                .setParameter("codCli", clienteId)
                .executeUpdate();
    }
    
    @Override
    public List<Interesses> findAll() {
        return em.createNamedQuery("Interesses.findAll", Interesses.class).getResultList();
    }      
}
